<template>
  <div class="login">
    <h1>登录</h1>
    <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label=" " class="login-btns">
        <el-button @click="resetLoginForm">重置</el-button>
        <el-button type="primary" @click="handleSubmit(1)">登录</el-button>
      </el-form-item>
    </el-form>
    <div class="other-login">
      <span class="text">其他登录方式：</span>
      <el-button class="wechat-login" @click="loginForType(loginTypes.wechat)">微信登录</el-button>
      <el-button class="tourists-login" @click="loginForType(loginTypes.tourists)">游客登录</el-button>
    </div>
  </div>
</template>

<script>
import myaxios from "../plugs/myaxios";
import {ElMessage} from "element-plus";
import store from "../store";
import CryptUtil from "../utils/CryptUtil";

export default {
  name: "Login",
  data() {
    return {
      form: {
        username: 'username',
        password: 'qwer1234',
      },
      rules: {
        username: [{required: true, message: '请输入账户', trigger: 'blur'},
          {min: 3, max: 20, message: '密码长度在 3 到 20 个字符之间'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 20, message: '密码长度在 4 到 32 个字符之间'}],
      },
      loginTypes: {
        wechat: "wechat",
        tourists: "tourists"
      }
    };
  },
  methods: {
    handleSubmit(identityType) {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          let pw = CryptUtil.encrypt(this.form.password, this.form.username + '.this_pw')
          myaxios.post('/dist/login', {
            username: this.form.username,
            password: pw,
            identityType: identityType
          }).then(rep => {
            if (!this.$store.state.loginDialogFlag) {
              this.$router.push({path: "/"})
              this.$store.dispatch('setLoginPageFlag', false)
            } else {
              this.$store.dispatch('setLoginDialogFlag', false)
            }
            let userInfo = this.$store.state.userInfo
            console.log(userInfo)
            userInfo.loginDateTime = new Date()
            console.log(userInfo)
            this.$store.dispatch('setUserInfo', userInfo)
            this.getUserInfo()
          }, err => {
            console.log(err)
            ElMessage.error("登录失败。");
          });
        } else {
          return false;
        }
      });
    },
    getUserInfo() {
      myaxios.get('/dist/user/currentUser').then(rep => {
        if (rep.data.code !== 100) {
          ElMessage.error(rep.data.msg);
          return;
        }
        let user = rep.data.data
        if (user && user.sysImages && user.sysImages.imgUrl) {
          store.dispatch('setAvatarImgUrl', user.sysImages.imgUrl)
          localStorage.setItem("avatarImgUrl", user.sysImages.imgUrl)
        }
      })
    },
    resetLoginForm() {
      console.log(Date.now())
      this.$refs.loginForm.resetFields()
    },
    loginForType(logInType) {
      console.log(logInType)
      console.log(this.$store.state.userInfo)
      switch (logInType) {
        case this.loginTypes.tourists:
          this.form.username = "test1234"
          this.form.password = "test1243=fdjj"
          this.handleSubmit(2)
          break
        case this.loginTypes.wechat:
          ElMessage.warning("暂时不支持微信登录。")
      }
    },
    test() {
      let userInfo = this.$store.state.userInfo
      console.log(userInfo.loginDateTime, userInfo.loginDateTime.getDate())
    }
  },
};
</script>

<style scoped>
.login {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 18px;
}

.login-form {
  margin-top: 20px;
}

.login-btns {
  display: flex;
  justify-content: center;
}

.other-login {
  margin-top: 20px;
  display: flex;
  align-items: center;
}

.text {
  font-size: 14px;
  margin-right: 10px;
}

.wechat-login {
  background-color: #00bb29;
  color: #fff;
}

.tourists-login {
  background-color: #03d764;
  color: #fff;
}
</style>